package com.fashionista;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.util.Log;

/**
 * A class with methods to parse an xml file and put results in an ArrayList.
 * 
 * @author Trevor Adelman, Patrick Koperwas, Tong Wu, Shayan Yassami
 * @version for cs48 group project, 3/11/2011
 */

public class ClothingParser {
	private static final String tag = "ClothingParser";
	private static final String FILE_EXTENSION = ".png";

	private DocumentBuilderFactory factory;
	private DocumentBuilder builder;
	private final List<ClothingItems> list;
	Integer [] clothingJacket;
	
	/**
	 * Constructor for class ClothingParser
	 */
	public ClothingParser() {
		list = new ArrayList<ClothingItems>();
	}
	
	private String getNodeValue(NamedNodeMap map, String key) {
		String nodeValue = null;
		Node node = map.getNamedItem(key);
		if (node != null) {
			nodeValue = node.getNodeValue();
		}
		return nodeValue;
	}

	/**
	 * Method to retrieve the list of ClothingItems objects
	 * 
	 * @return The list of ClothingItems objects. Each has clothing name, type, and weather variables
	 */
	public List<ClothingItems> getList() {
		return list;
	}

	/**
	 * Method to scan through each jacket item of a specific type
	 * 
	 * @param type The type of jacket to scan for
	 * @return an array of R.drawable Integer values
	 */
	public Integer [] getJacket(String type){
		ClothingItems item;
		int arraySize = 0;
		for(int i=0; i<list.size(); i++){
			item = list.get(i);
			if(item.getWeather().equals(type)){
				arraySize++;
			}
		}
		if (arraySize == 0){
			clothingJacket = new Integer[1];
			return clothingJacket;
		}
		Integer [] clothingJacket = new Integer[arraySize];
		int position = 0;
		for(int j=0; j<list.size(); j++){
			item = list.get(j);
			if(item.getWeather().equals(type)){
				clothingJacket[position] = item.getID();
				position++;
			}
		}
		return clothingJacket;
	}
	
	/**
	 * Method to scan through each pants item of a specific type
	 * 
	 * @param type The type of pants to scan for (shorts or not)
	 * @return an array of R.drawable Integer values
	 */
	public Integer [] getPants(String type){
		String type2 = "pants";
		if(type == "shorts"){
			type2 = "shorts";
		}
		ClothingItems item;
		int arraySize = 0;
		for(int i=0; i<list.size(); i++){
			item = list.get(i);
			if(item.getType().equals(type2)){
				arraySize++;
			}
		}
		Integer [] clothingPants = new Integer[arraySize];
		int position = 0;
		for(int j=0; j<list.size(); j++){
			item = list.get(j);
			if(item.getType().equals(type2)){
				clothingPants[position] = item.getID();
				position++;
			}
		}
		return clothingPants;
	}
	
	/**
	 * Method to parse an xml file for clothing attributes
	 * @param inStream The source to be parsed
	 */
	public void parse(InputStream inStream) {
		try {
			factory = DocumentBuilderFactory.newInstance();
			builder = factory.newDocumentBuilder();
			builder.isValidating();
			Document doc = builder.parse(inStream, null);

			doc.getDocumentElement().normalize();

			NodeList clothingList = doc.getElementsByTagName("clothing"); //Tells the method to create a NodeList of all the items with the "clothing" tag in the xml file
			final int length = clothingList.getLength(); //and sets the length equal to the length of the NodeList for later use in the for loop

			for (int i = 0; i < length; i++) {
				final NamedNodeMap attr = clothingList.item(i).getAttributes(); //Gets the attributes of the item at position i
				final String clothingName = getNodeValue(attr, "names"); //Looks in the source for name, type, and weather attributes
				final String clothingType = getNodeValue(attr, "type"); //and sets them to String variables: clothingName, clothingType, and clothingWeather
				final String clothingWeather = getNodeValue(attr, "weather");
				final String clothingID = getNodeValue(attr, "id");

				ClothingItems clItems = new ClothingItems(clothingName,
						clothingType, clothingWeather, clothingID, clothingName
								+ FILE_EXTENSION); //Makes a new ClothingItems object for each entry

				list.add(clItems); //Adds the object to an ArrayList to be used later

				Log.d(tag, clItems.toString());
			}
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}
}